﻿using System.Collections.Generic;
using System.Web;
using eCommerce.db.Action;

namespace eCommerce.cms
{
    /// <summary>
    /// Summary description for PostRequest
    /// </summary>
    public class PostRequest : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            IBaseAction action = GetAction(context.Request.Form["m"].Trim().ToLower());            
            if (action != null)
                action.Do(context);
        }        

        static PostRequest()
        {
            RegisterAction("comment", new CommentAction());
            RegisterAction("category", new CategoryAction());
            RegisterAction("article", new ArticleAction());
            RegisterAction("product", new ProductAction());
            RegisterAction("order", new OrderAction());
            RegisterAction("orderdetail", new OrderDetailAction());         
            RegisterAction("customer", new CustomerAction());
            RegisterAction("note", new NoteAction());
            RegisterAction("size", new SizeAction());
            RegisterAction("ie", new IeAction());
            RegisterAction("iedetail", new IeDetailAction());              
            RegisterAction("store", new StoreAction());              
        }

        #region private

        // Fields
        private static readonly IDictionary<string, IBaseAction> Dicts = new Dictionary<string, IBaseAction>();

        protected static IBaseAction GetAction(string actionName)
        {
            IBaseAction action;
            if (Dicts.TryGetValue(actionName, out action))
            {
                return action;
            }
            return null;
        }

        protected static void RegisterAction(string actionName, IBaseAction action)
        {
            Dicts[actionName] = action;
        }

        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}